package com.softcits.email.common;

import java.util.Comparator;
import java.util.Date;

import com.softcits.email.model.FileInfo;

public class FileInfoComparator implements Comparator<FileInfo> {
    
    public FileInfoComparator() {
        
    }
    
    
    public FileInfoComparator(String columnName, String dir) {
        this.columnName = columnName;
        this.dir = dir;
    }
    
    private String columnName = "";
    
    private String dir;

    @Override
    public int compare(FileInfo f1, FileInfo f2) {
        
        
        int cres = compareTo(f1, f2);
        int result;
        if("desc".equalsIgnoreCase(dir)) {
            result = -cres;
        } else if("asc".equalsIgnoreCase(dir)) {
            result = cres;
        } else {
            result = 0;
        }
        
        return result;
    }
    
    public int compareTo(FileInfo f1, FileInfo f2) {
        switch (columnName) {
        case "modificationTime":
            Date md1 = f1.getModificationTime();
            Date md2 = f2.getModificationTime();
            return md1.compareTo(md2);
        case "size":
            Long s1 = f1.getSize();
            Long s2 = f2.getSize();
            if(s1 == null) {
                s1 = 0L;
            }
            if(s2 == null) {
                s2 = 0L;
            }
            
            return (int) (s1 - s2);

        default:
            break;
        }
        
        return 0;
    }

}
